package com.smartstate.IOC.controller;

import com.google.common.collect.Lists;
import com.smartstate.IOC.util.StringDateUtills;
import com.smartstate.common.web.httpResult.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
@Api(tags = {"产品态势"})
public class ProductController {

    @RequestMapping(value = "/getProduceProportion", method = RequestMethod.GET)
    @ApiOperation(value = "产品态势-园区生产占比")
    public ResultVO<List<Map<String,Object>>> getProduceProportion(){
        List<String> dayStrs = Lists.newArrayList("今日", "昨日", "前日");
        List<String> deviceNameStrs = Lists.newArrayList("L1", "L2", "L3");
        int produceNum = 19888;
        List<Map<String, Object>> result = Lists.newArrayList();
        int total = produceNum * deviceNameStrs.size();
        //List<Map<String,List<>>>>
        for(String dayStr : dayStrs){
            Map<String, Object> item = new HashMap<>();
            List<Map<String, Object>> deviceProduceInfos = Lists.newArrayList();
            for(String deviceName : deviceNameStrs){
                Map<String, Object> innerItem = new HashMap<>();
                innerItem.put("deviceName", deviceName);
                innerItem.put("produceNum", produceNum);
                Double proportion = (produceNum / (double)total)*100;
                innerItem.put("proportion", proportion);
                deviceProduceInfos.add(innerItem);
            }
            item.put(dayStr, deviceProduceInfos);
            result.add(item);
        }
        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getGroupProducts", method = RequestMethod.GET)
    @ApiOperation(value = "产品态势-各工序产量统计")
    public ResultVO<List<Map<String,Object>>> getGroupProducts(){
        List<String> dayStrs = Lists.newArrayList("今日", "昨日", "前日");
        List<String> groupNames = Lists.newArrayList("设备组", "电气组", "运维组", "通讯组");
        int produceNum = 18;
        List<Map<String, Object>> result = Lists.newArrayList();
        for(String dayStr : dayStrs){
            HashMap map = new HashMap();
            List<Map<String, Object>> groupProduceInfos = Lists.newArrayList();
            for(String groupName : groupNames){
                Map<String, Object> innerItem = new HashMap<>();
                innerItem.put(groupName, produceNum);
                groupProduceInfos.add(innerItem);
            }
            map.put(dayStr, groupProduceInfos);
            result.add(map);
        }
        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getDayProducts", method = RequestMethod.GET)
    @ApiOperation(value = "产品态势-总产量统计")
    public ResultVO<List<Map<String,Object>>> getDayProducts(){
        Long currTime = new Date().getTime();
        Long dayMills = 24 * 60 * 60 * 1000L;
        int productsNum = 18;
        List<Map<String,Object>> result = Lists.newArrayList();
        for(int i = 0 ; i < 4 ; i ++){
            HashMap dayProducts = new HashMap();
            dayProducts.put("day", StringDateUtills.getDayStyleStrFromDateTime(new Date(currTime - dayMills * i)));
            dayProducts.put("countNum", productsNum);
            result.add(dayProducts);
        }
        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getStorageProducts", method = RequestMethod.GET)
    @ApiOperation(value = "产品态势-仓库产品数量")
    public ResultVO<List<Map<String,Object>>> getStorageProducts(){
        List<Map<String,Object>> result = Lists.newArrayList();

        Map<String,Object> map1 = new HashMap<>();
        map1.put("storageName", "仓1");
        map1.put("materialType", "原料");
        map1.put("materialNum", 1000);
        map1.put("storageUsage", "32");
        result.add(map1);

        Map<String,Object> map2 = new HashMap<>();
        map2.put("storageName", "仓2");
        map2.put("materialType", "前期半成品");
        map2.put("materialNum", 70000);
        map2.put("storageUsage", "32");
        result.add(map2);

        Map<String,Object> map3 = new HashMap<>();
        map3.put("storageName", "仓3");
        map3.put("materialType", "中期半成品");
        map3.put("materialNum", 2300);
        map3.put("storageUsage", "32");
        result.add(map3);

        Map<String,Object> map4 = new HashMap<>();
        map4.put("storageName", "仓4");
        map4.put("materialType", "成品");
        map4.put("materialNum", 600);
        map4.put("storageUsage", "32");
        result.add(map4);

        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getStorageOutPutRecords", method = RequestMethod.GET)
    @ApiOperation(value = "产品态势-出入库实时明细")
    public ResultVO<List<Map<String,Object>>> getStorageOutPutRecords(){
        Long currTime = new Date().getTime();
        Long secondMills = 10 * 1000L;
        int productsNum = 1888;
        List<Map<String,Object>> result = Lists.newArrayList();
        List<String> deviceNameStrs = Lists.newArrayList("L1", "L2", "L3");
        for(int i = 0 ; i < 15 ; i ++){
            HashMap dayProducts = new HashMap();
            dayProducts.put("time", StringDateUtills.getSecondStyleStrFromDateTime(new Date(currTime - secondMills * i)));
            dayProducts.put("num", productsNum);
            dayProducts.put("storageName", "库房" + (i % 5) + 1);
            dayProducts.put("productName", deviceNameStrs.get(i % 3));
            String type = "入库";
            if(i % 3 == 0)type = "出库";
            dayProducts.put("type", type);
            result.add(dayProducts);
        }
        return ResultVO.success(result);
    }

    @RequestMapping(value = "/getProductExcellentRate", method = RequestMethod.GET)
    @ApiOperation(value = "产品态势-各个工序良品率")
    public ResultVO<List<Map<String,Object>>> getProductExcellentRate(){
        Long currTime = new Date().getTime();
        Long dayMills = 24 * 60 * 60 * 1000L;
        int excellentRate = 98;
        List<Map<String,Object>> result = Lists.newArrayList();
        List<String> deviceNameStrs = Lists.newArrayList("L1", "L2", "L3");
        for(String deviceName: deviceNameStrs){
            HashMap productDayRates = new HashMap();
            List<Map<String, Object>> dayRates = Lists.newArrayList();
            for(int i = 0 ; i < 6 ; i ++){
                Map<String, Object> dayRate = new HashMap<>();
                dayRate.put(StringDateUtills.getDayStyleStrFromDateTime(new Date(currTime - dayMills * i)), excellentRate);
                dayRates.add(dayRate);
            }
            productDayRates.put(deviceName, dayRates);
            result.add(productDayRates);
        }
        return ResultVO.success(result);
    }

}
